package com.vernhe.list;


public class ArrayList<E> extends AbstractList<E>{
    /**
     * 所以的元素
     */
    private E[] elements;
    private static final int DEFAULT_CATPACITY = 10;

    public ArrayList(int capacity) {
        capacity = capacity < DEFAULT_CATPACITY ? DEFAULT_CATPACITY : capacity;
        elements = (E[]) new Object[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CATPACITY);
    }

    /**
     * 得到index位置的元素
     * @param index
     * @return
     */
    @Override
    public E get(int index) {
        /**
         * 最好：O(1)
         * 最坏：O(1)
         * 平均：O(1)
         */
        rangeCheck(index);
        return elements[index];
    }

    /**
     * 设置index位置的元素
     * @param index
     * @param element
     * @return
     */
    @Override
    public E set(int index, E element) {
        /**
         * 最好：O(1)
         * 最坏：O(1)
         * 平均：O(1)
         */
        rangeCheck(index);
        E old = elements[index];
        elements[index] = element;
        return old;
    }

    /**
     * 获取元素的位置
     * @param element
     * @return
     */
    public int indexOf(E element) {
        if (element == null){
            for (int i = 0; i < size; i++) {
                if (elements[i] == null) return i;
            }
        }else {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i])) return i;
            }
        }

        return ELEMENT_NOT_FOUND;
    }

    /**
     * 清除
     */
    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    /**
     * 删除元素
     * @param index
     * @return
     */
    @Override
    public E remove(int index) {
        /**
         * 最好：O(1)
         * 最坏：O(N)
         * 平均：O(N)
         */
        rangeCheck(index);
        E old = elements[index];
        for(int i = index + 1; i < size ; i++){
            elements[i-1] = elements[i];
        }
        elements[--size] = null;
        return old;
    }

    /**
     * 删除元素
     * @param element
     * @return
     */
    public E remove(E element) {
        return remove(indexOf(element));
    }

    /**
     * 添加元素
     * @param index
     * @param element
     */
    public void add(int index,E element) {
        /**
         * 最好：O(1)
         * 最坏：O(N)
         * 平均：O(N)
         */
        rangeCheckForAdd(index);

        ensureCapacity(size+1);
        for (int i = size; i > index; i--) {
            elements[i] = elements[i-1];
        }
        elements[index] = element;
        size++;
    }

    private void ensureCapacity(int capacity) {
        int oldCapcity = elements.length;
        //容量够用
        if (oldCapcity >= capacity) return;
        //扩容为旧容量的1.5倍
        int newCapacity = oldCapcity + (oldCapcity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }



    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=" + size).append(",[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(",");
            }
            string.append(elements[i]);
        }
        string.append("]");
        return string.toString();
    }

}
